﻿using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Unsubscribe;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SelfMadeMillionaire.Core.Entities;

namespace SelfMadeMillionaire.Web.Infrastructure.Controllers
{
    public class UnsubscribeController : BaseController
    {
        ISubscriberService _subscriberService;

        public UnsubscribeController(ISubscriberService subscriberService)
        {
            _subscriberService = subscriberService;
        }


        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(UnsubscribeViewModel model)
        {
            ClearProductTypeInSession();

            var subscriber = _subscriberService.Get(model.Email);
            if ((subscriber != null) && (subscriber.Status == (byte)StatusType.Active))
            {
                ModelState.Remove("Email");
                model.Email = "";

                _subscriberService.Unsubscribe(subscriber);
            }

            model.Message = "You have been successfully unsubscribed";
            _subscriberService.UnsubscribeSP(model.Email);
            return View(model);
        }

    }
}
